package mo.daliy;

import java.util.Map;
import java.util.TreeMap;

public class L3169 {

    public int countDays(int days, int[][] meetings) {
        TreeMap<Integer, Integer> arr = new TreeMap<>();
        for (int i = 0; i < meetings.length; i++) {
            arr.put(meetings[i][0], arr.getOrDefault(meetings[i][0], 0)+1);
            arr.put(meetings[i][1]+1, arr.getOrDefault(meetings[i][1]+1, 0)-1);
        }
        arr.put(days+1, arr.getOrDefault(days+1, 0)+1);
        int sum = 0;
        int cur = 1;
        int total = 0;
        for (Map.Entry<Integer, Integer> entry : arr.entrySet()) {
            if (sum == 0 && sum + entry.getValue() > 0) {
                total += entry.getKey() - cur;
            } else if (sum > 0 && sum + entry.getValue() == 0) {
                cur = entry.getKey();
            }

            sum += entry.getValue();
        }

        return total;

    }

}
